#!/bin/bash

echo "修改configure.ac - 添加lib/Makefile到AC_CONFIG_FILES："
grep -q 'AC_CONFIG_FILES(\[lib\/Makefile' configure.ac || (sed -n 's/AC_CONFIG_FILES(\[/AC_CONFIG_FILES(\[lib\/Makefile\n\                 /p' configure.ac && sed -i 's/AC_CONFIG_FILES(\[/AC_CONFIG_FILES(\[lib\/Makefile\n\                 /' configure.ac)
echo
echo "修改configure.ac - 在AC_PROG_CC之后添加gl_EARLY和gl_INIT："
grep -q '^gl_EARLY$' configure.ac || (sed -n '/^AC_PROG_CC$/ a gl_EARLY' configure.ac && sed -i '/^AC_PROG_CC$/ a gl_EARLY' configure.ac)
grep -q '^gl_INIT$' configure.ac || (sed -n '/^gl_EARLY$/ a gl_INIT' configure.ac && sed -i '/^gl_EARLY$/ a gl_INIT' configure.ac)
echo
echo "修改configure.ac - 添加deplibs_check_method=\"pass_all\"："
grep -q 'deplibs_check_method="pass_all"' configure.ac || (sed -n '/^AC_PROG_LIBTOOL$/ a deplibs_check_method="pass_all"' configure.ac && sed -i '/^AC_PROG_LIBTOOL$/ a deplibs_check_method="pass_all"' configure.ac)

echo

echo "修改Makefile.am文件 - 添加lib到SUBDIRS："
grep -q 'SUBDIRS\s*=\s*lib ' Makefile.am || (sed -n 's/\(SUBDIRS\s*=\)\s*/\1 lib /p' Makefile.am &&  sed -i 's/\(SUBDIRS\s*=\)\s*/\1 lib /' Makefile.am)
echo
echo "修改Makefile.am文件 - 添加m4/gnulib-cache.m4到EXTRA_DIST："
grep -q 'EXTRA_DIST\s*=\s*m4/gnulib-cache.m4 ' Makefile.am || (sed -n 's/\(EXTRA_DIST\s*=\)\s*/\1  m4\/gnulib-cache.m4 /p' Makefile.am && sed -i 's/\(EXTRA_DIST\s*=\)\s*/\1  m4\/gnulib-cache.m4 /' Makefile.am)
echo
echo "修改Makefile.am文件 - 添加gnulib包含路径和链接库："
for file in $(find src utils tests -type f -name "Makefile.am"); do
    echo "Makefile.am: $file"
    grep -q 'AM_CPPFLAGS\s*=' $file && (\
        grep -q 'AM_CPPFLAGS = -I\$(top_builddir)\/lib -I\$(top_srcdir)\/lib' $file || \
        (sed -n 's/\(AM_CPPFLAGS\s*\)=/AM_CPPFLAGS = -I\$(top_builddir)\/lib -I\$(top_srcdir)\/lib\n\1+=/p' $file && sed -i 's/\(AM_CPPFLAGS\s*\)=/AM_CPPFLAGS = -I\$(top_builddir)\/lib -I\$(top_srcdir)\/lib\n\1+=/' $file) \
    )
    grep -q '@GLIB2_LIBS@' $file && (\
        grep -q '@GLIB2_LIBS@ \$(top_builddir)\/lib\/libgnu\.a' $file || \
        (sed -n 's/@GLIB2_LIBS@/@GLIB2_LIBS@ \$(top_builddir)\/lib\/libgnu.a/p' $file && sed -i 's/@GLIB2_LIBS@/@GLIB2_LIBS@ \$(top_builddir)\/lib\/libgnu.a/' $file) \
    )
    echo
done

echo "修改.cpp文件 - 添加#include \"config.h\"："
for file in $(find src utils tests -type f -name "*.cpp"); do
    echo "cpp: $file"
    grep -q '#include\s*["<]config.h[">]' $file || \
    (sed -n '0,/#include/{s/#include/#include "config.h"\n#include/p}' $file && sed -i '0,/#include/{s/#include/#include "config.h"\n#include/}' $file)
    echo
done

echo "修改.h和.cpp文件 - 为fopen和open函数添加二进制打开标志："
for file in $(find src utils tests -type f -name "*.cpp" -o -name "*.h"); do
    echo "$file"
    sed -n 's/fopen\s*(\s*\(.\+\)\s*,\s*"\([rw]\)"\s*)/fopen(\1, "\2b")/p' $file && sed -i 's/fopen\s*(\s*\(.\+\)\s*,\s*"\([rw]\)"\s*)/fopen(\1, "\2b")/' $file
    sed -n 's/\(O_TRUNC\|O_RDONLY\|O_CREATE\|O_WRONLY[,)]\)/\1|O_BINARY/p' $file && sed -i 's/\(O_TRUNC\|O_RDONLY\|O_CREATE\|O_WRONLY[,)]\)/\1|O_BINARY/' $file
done
